package models

import (
	"go_web_todo/cache"
	"strconv"

	"gorm.io/gorm"
)

//任务模型
type Task struct {
	gorm.Model
	//User      User   `gorm:"ForeignKey:User;AssociationForeignKey:ID"`
	User      User   `gorm:"ForeignKey:Uid;AssociationForeignKey:ID"`
	Uid       uint   `gorm:"not null"`
	Title     string `gorm:"index;not null"`
	Status    int    `gorm:"default:0"`
	Content   string `gorm:"type:longtext"`
	StartTime int64
	EndTime   int64 `gorm:"default:0"`
}

func (task *Task) View() uint64 {
	//增加点击数
	countStr, _ := cache.RedisClient.Get(cache.TaskViewKey(task.ID)).Result()
	count, _ := strconv.ParseUint(countStr, 10, 64)
	return count
}

func (task *Task) AddView() {
	cache.RedisClient.Incr(cache.TaskViewKey(task.ID))                      //增加视频点击数
	cache.RedisClient.ZIncrBy(cache.RankKey, 1, strconv.Itoa(int(task.ID))) //增加排行点击数
}
